<template>
  <div class="content">
    <el-form :inline="true" :model="formInline">
      <el-form-item>
        <el-button type="primary" @click="dialogVisible=true" plain>新增角色</el-button>
        <el-button @click="delFn" type="danger" :disabled="delParams.ids.length==0">批量删除</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableData.records" @selection-change="handleSelectionChange" border stripe>
      <el-table-column type="selection" width="55" />
      <el-table-column type="index" />
      <el-table-column prop="name" label="名称" align="center" />
      <el-table-column prop="des" label="备注" align="center" />
      <el-table-column label="创建时间" min-width="170" align="center">
        <template #default="scope">
          {{$utils.dateTimeFormateFn(scope.row.createTimeLong,"all")}}
        </template>
      </el-table-column>
      <el-table-column label="更新时间" min-width="170" align="center">
        <template #default="scope">
          {{$utils.dateTimeFormateFn(scope.row.updateTimeLong,"all")}}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" min-width="150">
        <template #default="scope">
          <el-button type="primary" text @click="handleClick(scope.row,1)">编辑</el-button>
          <el-button type="primary" text @click="handleClick(scope.row,2)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-if="tableData['total']" :total="tableData['total']" v-model:page="formInline.pageNum" v-model:limit="formInline.pageSize" @pagination="getInfo" />

    <el-dialog v-model="dialogVisible" title="新增/编辑角色" width="600" :before-close="handleClose">
      <el-form label-width="100" :model="form" :rules="rules" ref="formEl">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" clearable />
        </el-form-item>
        <el-form-item label="菜单">
          <!-- <menuTreeSelect v-model="form.menuIds" :expandAll="1"></menuTreeSelect> -->
          <menuTree expandAll="1" v-model="form.menuIds"></menuTree>
        </el-form-item>
        <el-form-item prop="des" label="备注">
          <el-input v-model="form.des" placeholder="请输入备注" type="textarea" :autosize="{ minRows : 4 }" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="submitForm" :loading="loading">
            确 认
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { onMounted } from "vue";
const { proxy } = getCurrentInstance();
import menuTreeSelect from "@/components/SystemMain/menuTreeSelect.vue"; //菜单

let dialogVisible = ref(false);
let tableData = ref({});
let formInline = ref({
  pageNum: 1,
  pageSize: 10,
  search: null,
});
let loading = ref(false);
let delParams = ref({
  ids: [],
});
let form = ref({
  name: "",
  des: "",
  menuIds: [],
});
const rules = reactive<FormRules<RuleForm>>({
  name: [{ required: true, message: "请输入名称", trigger: "blur" }],
});

onMounted(() => {
  search();
});

let search = () => {
  formInline.value.pageNum = 1;
  getInfo();
};
let getInfo = async () => {
  tableData.value = await proxy.$apis.roleList(formInline.value);
};
let init = () => {
  formInline.value = {
    pageNum: 1,
    pageSize: 10,
    accountSearch: null,
    userNameSearch: null,
  };
  getInfo();
};
let handleSelectionChange = (e) => {
  let ids = [];
  e.forEach((element) => {
    ids.push(element.id);
  });
  delParams.value.ids = [...ids];
};

let delFn = async () => {
  let resp = await proxy.$utils.confirmTips("是否确定删除?");
  if (!resp) return;
  await proxy.$apis.roleDel(delParams.value);
  proxy.$utils.showToast("删除成功");
  init();
};

let handleClick = async (row, num: Number) => {
  switch (num) {
    case 1:
      let resp = await proxy.$apis.roleDetail({ id: row.id });
      form.value = proxy.$utils.formatFunction(resp, { ...form.value });
      form.value.id = row.id;
      dialogVisible.value = true;
      break;
    case 2:
      delParams.value.ids = [row.id];
      delFn();
      break;
    default:
      break;
  }
};
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!proxy.$refs.formEl) return;
  await proxy.$refs.formEl.validate((valid, fields) => {
    if (valid) {
      save();
    } else {
      console.log("error submit!", fields);
    }
  });
};
let save = async () => {
  try {
    loading.value = true;
    if (form.value.id) {
      await proxy.$apis.roleUpdate(form.value);
    } else {
      await proxy.$apis.roleAdd(form.value);
    }
    proxy.$utils.showToast("操作成功");
    handleClose();
  } catch (error) {
  } finally {
    loading.value = false;
  }
};

let handleClose = () => {
  form.value = {
    name: "",
    des: "",
    menuIds: [],
  };
  proxy.$refs.formEl.resetFields();
  getInfo();
  dialogVisible.value = false;
};
</script>

<style lang="scss" scoped>
.content {
  margin: 20px;
}
</style>
